package com.example.controller;

import com.example.dto.UserDto;
import com.example.loader.HazelcastMapStoreLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/***
 *
 * 测试MAP的信息
 */
@RestController
public class TestMapController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private HazelcastMapStoreLoader hazelcastMapStoreLoader;

    /****
     * 设置一个对象
     * @return
     */
    @RequestMapping("put")
    public String put() {
        String uuid = UUID.randomUUID().toString();
        UserDto userDto = new UserDto();
        userDto.setId(uuid);
        userDto.setName(uuid);
        this.hazelcastMapStoreLoader.getMap().put(uuid, userDto);
        return uuid;
    }


    /****
     * 设置有过期时间的map对象
     * @return
     */
    @RequestMapping("putTtl")
    public String putTtl() {
        String uuid = UUID.randomUUID().toString();
        UserDto userDto = new UserDto();
        userDto.setId(uuid);
        userDto.setName(uuid);
        this.hazelcastMapStoreLoader.getMap().put(uuid, userDto, 10, TimeUnit.SECONDS);
        return uuid;
    }

    /****
     * 获取一个对象
     * @param key
     * @return
     */
    @RequestMapping("get")
    public Object get(String key) {
        return this.hazelcastMapStoreLoader.getMap().get(key);
    }

    /***
     * 获取所有map的key
     * @return
     */
    @RequestMapping("getAll")
    public Object getAll() {
        return this.hazelcastMapStoreLoader.getMap().keySet();
    }

    /***
     * 删除map对象
     * @param key
     * @return
     */
    @RequestMapping("remove")
    public Object remove(String key) {
        return this.hazelcastMapStoreLoader.getMap().remove(key);
    }
}
